﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace CLFramework.EnvironmentCheck
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnStartCheck_Click(object sender, EventArgs e)
        {
            this.lblResult.Text = string.Empty;

            List<ICheckItem> list = LoadCheckItems();

            foreach (var ci in list.OrderBy(i => i.Order))
            {
                ListViewItem item = new ListViewItem(ci.Name);
                item.SubItems.Add(ci.Result ? "√" : "×");
                item.SubItems.Add(ci.Remarks);
                lvColumns.Items.Add(item);
            }

            string resultmsg = string.Empty;
            if (list.TrueForAll(i => i.Must ? i.Result : true))
            {
                resultmsg = ResultMsg.应该能够顺利运行软件.ToString();
                lblResult.ForeColor = Color.Green;
            }
            else
            {
                resultmsg = ResultMsg.您的系统存在严重问题_估计不能够正常运行软件.ToString();
                lblResult.ForeColor = Color.Red;
            }
            lblResult.Text = "检查结果：" + resultmsg;
        }

        private List<ICheckItem> LoadCheckItems()
        {
            List<ICheckItem> items = new List<ICheckItem>();
            List<Type> types = Assembly.GetExecutingAssembly().GetTypes()
                .Where(t => t.GetInterface("ICheckItem") != null)
                .ToList();

            foreach (var type in types)
            {
                object item = Activator.CreateInstance(type);
                items.Add(item as ICheckItem);
            }

            return items;
        }

        public enum ResultMsg
        {
            应该能够顺利运行软件,
            您的系统存在一些问题_仍能够使用软件的大部分功能,
            您的系统存在严重问题_估计不能够正常运行软件
        }
    }
}
